#encoding=utf8

import aaf
from optparse import OptionParser

def printProperties(AAFObject,tab=""):
    if "AAFFraction" in str(type(AAFObject)):
        return
    if "AUID" in str(type(AAFObject)):
        return
    if "MobID" in str(type(AAFObject)):
        return
    if "TypeDefRecord" in str(type(AAFObject)):
        return
    for object_key in AAFObject.all_keys():
        property = AAFObject.get(object_key)
        key_name = str(object_key)
        key_type = type(property)
        key_value= AAFObject.get_value(object_key)
        # print tab + "[" + key_name + "]" + "(" + str(key_type) + "):" + str(key_value)
        print tab + "[" + key_name + "]" + ":" + str(key_value)
        # print type(key_value)
        if "aaf" in str(type(key_value)):
            if "PropValueResolveIter" not in str(type(key_value)):
                printProperties(key_value,tab=tab + "       ")
            else:
                for index in range(len(key_value)):
                    pro_object = key_value[index]
                    if "int" in str(type(pro_object)):
                        continue
                    pro_type = type(key_value[index])
                    pro_name = pro_object.name
                    if "NoneType" not in str(type(key_value[index])):
                        print tab + "       " + key_name + "[" + str(index) + "]." + str(key_value[index].name) + "(" + str(type(key_value[index])) + "):" + str(key_value[index])
                        if "aaf" in str(type(key_value[index])):
                                printProperties(key_value[index],tab + "                ")
    pass

def walk_properties(space, iter_item):
    parser = OptionParser()
    parser.add_option('--show_dictionary', action='store_true', default=True, dest='show_dict',
                      help="show dictionary properties")
    parser.add_option('--show_stream', action='store_true', default=False, dest='show_stream',
                      help="show binary stream data")
    (options, args) = parser.parse_args()

    for item in iter_item:
        value = item
        if isinstance(item, aaf.property.PropertyItem):
            value = item.value
        name = ""

        if hasattr(item, 'name'):
            name = item.name or ""

        print(space, name, value)

        if isinstance(value, aaf.dictionary.Dictionary) and not options.show_dict:
            continue
        # don't dump out stream data, its ugly
        if isinstance(value, aaf.iterator.TypeDefStreamDataIter) and not options.show_stream:
            continue

        s = space + '   '
        if isinstance(value, aaf.base.AAFObject):
            walk_properties(s, value.properties())
        if isinstance(value, aaf.iterator.BaseIterator):
            walk_properties(s, value)
        pass



output_aaf = 'resources/aaf_create_rw.aaf'
aaf_file = aaf.open(output_aaf, 'r')
print "[aaf_file]:" + str(aaf_file)
print

class_iid = aaf_file.class_iid
header = aaf_file.header
storage = aaf_file.storage
dictionary = aaf_file.dictionary

print "[aaf_file.class_iid]:" + str(class_iid)
print "[aaf_file.header]:" + str(header)
print "[aaf_file.storage]:" + str(storage)
print "[aaf_file.dictionary]:" + str(dictionary)
print

content = header.get_value("Content")
mobs = content.get_value("Mobs")

aaf_mobs = storage.mobs()

for aaf_mob in aaf_mobs:
    print aaf_mob
    if "CompositionMob" in str(type(aaf_mob)):

        #CompositionMob下面有多个Slot，添加TimelineMobSlot方式
        # timecode = Timecode(aaf_file,100,0,24)
        # 添加TimelineMobSlot，当前理解为Track
        # segment = Sequence(aaf_file,media_kind="picture")
        # aaf_mob.append_new_timeline_slot(24,segment,slot_name="Track")
        # 未知效果
        # aaf_mob.create_clip()
        # 给Mob的属性key值为Attributes的对象，里面添加key,value
        # aaf_mob.append_attribute('testValue','Composition TimeLine')
        # 获取Mob的数据
        # name = aaf_mob.get_value("Name")
        print aaf_mob
        # storage.remove_mob(aaf_mob)
        # storage.add_mob(aaf_mob)

        for slot in aaf_mob.slots():
            print "    " + str(slot) + " " + str(type(slot))

            if "TimelineMobSlot" in str(type(slot)):
                timelineMobSlot = slot
                print "    " + "[timelineMobSlot.name]:" + str(timelineMobSlot.name)
                print "    " + "[timelineMobSlot.media_kind]:" + str(timelineMobSlot.media_kind)
                print "    " + "[timelineMobSlot.physical_num]:" + str(timelineMobSlot.physical_num)
                print "    " + "[timelineMobSlot.mark_in]:" + str(timelineMobSlot.mark_in)
                print "    " + "[timelineMobSlot.mark_out]:" + str(timelineMobSlot.mark_out)
                print "    " + "[timelineMobSlot.user_pos]:" + str(timelineMobSlot.user_pos)
                print "    " + "[timelineMobSlot.segment]:" + str(type(timelineMobSlot.segment))
                segment = timelineMobSlot.segment
                print "    " + "    " + str(segment) + " " + str(type(timelineMobSlot))
                # if "DataDef_Picture" not in str(timelineMobSlot.media_kind):
                #     continue
                if "Sequence" in str(type(segment)):
                    sequence = segment
                    # 错误的用法，但是不知道如何使用
                    # positions = sequence.positions()
                    # index, edit_start_time, component = sequence.positions()
                    # print "[sequence.index]:" + str(index)
                    # print "[sequence.edit_start_time]:" + str(edit_start_time)
                    # print "[sequence.component]:" + str(component)
                    print "    " + "    " + "[sequence.count]:" + str(sequence.count)
                    for index in range(0, sequence.count):
                        print "    " + "    " + "[sequence.time_at_index(" + str(index) + ")]:" + str(sequence.time_at_index(index))
                        print "    " + "    " + "[sequence.component_at_time(" + str(index) + ")]:" + str(sequence.component_at_time(index))

                    for component in sequence.components():
                        # print "[sequence.component_at_time(" + str(type(component)) + ")]:" + str(sequence.component_at_time(component))
                        print "    " + "    " + "    " + str(component) + " " + str(type(component))
                        if "Filler" in str(type(component)):
                            filler = component
                            filler.length = filler.length + 1000
                            sequence.length = sequence.length + 1000


                        if "OperationGroup" in str(type(component)):
                            operationGroup = component
                            # inputsegments = component.input_segments()
                            # print "    " + "    " + "    " + "    " + str(inputsegments) + " " + str(type(inputsegments))
                            for input_segment in operationGroup.input_segments():
                                print "    " + "    " + "    " + "    " + str(input_segment) + " " + str(type(input_segment))
                                if "SourceClip" in str(type(input_segment)):
                                    sourceclip = input_segment
                                    start_time = sourceclip.start_time
                                    source_ref = sourceclip.source_ref
                                    print start_time
                                    print sourceclip.mob_id
                                    # print source_ref


aaf_file.save("resources/aaf_create_rw_filler1000.aaf")
print "[aaf_file]:" + str(aaf_file)

# dump result file
print "Dump result file Content:"
walk_properties("", aaf_file.header.properties())

aaf_file.close()